// pages/good-comment/good-comment.js
function getTimeLeft(createTime) {
	let time1 = Date.parse(new Date()); //毫秒
	let time2 = createTime;
	let mss = time1 - time2;
	if (mss > 0) {
		if (mss > 86400000) {
			//大于24小时
			return util.getLocalDate1(time2)
			// debugger
		} else {
			if (mss > 3600000) {
				//大于1小时
				var hours = parseInt(mss / (60 * 60 * 1000))
				// debugger
				return hours + '小时前'
			} else {
				if (mss > 60000) {
					//大于1分钟
					var minutes = parseInt(mss % (60 * 60 * 1000) / 60000);
					return minutes + '分钟前'
				} else {
					var seconds = parseInt(mss % 60000);
					return seconds + '秒前'
				}
			}
		}
	}
}

const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import {
	getCommentList
} from '../../utils/request/apiurl'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		commentList: [],
		goodId: null,
		searchGood: {
			page: 1,
			size: 10,
			pages:0,
		},
		isRequest: false,
		tasteList: [{
				id: 0,
				name: '全部评论',
				current: true
			},
			{
				id: 3,
				name: '好评',
				current: false
			},
			{
				id: 2,
				name: '中评',
				current: false
			},
			{
				id: 1,
				name: '差评',
				current: false
			}
		],
		id: 0, //评论id
		tasteNav: 0, //评价
		all:0,//全部评价条数

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			goodId: options.goodId
		});
		// this.comment();
		this.handleComment();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			commentList:[],
			searchGood: {
				page: 1,
				size: 10,
				pages:0,
			},
		})
		this.handleComment();
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
			this.setData({
			  searchGood: {
			    page: this.data.searchGood.page + 1,
			    size: 10,
			    pages: this.data.searchGood.pages
			  }
			})
			this.handleComment()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 选择分类
	changeTasteTab(event) {
		// console.log(event)
		const currentIndex = event.currentTarget.dataset.index;
		const currentId = event.currentTarget.dataset.id;
		// 给当前项增加class
		if (this.data.tasteNav !== currentIndex) {
			this.setData({
				tasteNav: currentIndex,
				id: currentId
			})
			this.handleComment();
		}
	},

	handleComment() {
		// this.setData({
		// 	isRequest: true
		// })
		if (this.data.id == 0) {
			var source = {
				id: this.data.goodId,
				page: this.data.searchGood.page,
				size: this.data.searchGood.size,
			}
		} else {
			var source = {
				id: this.data.goodId,
				status: this.data.id,
				page: this.data.searchGood.page,
				size: this.data.searchGood.size,
			}
		}
		api.post(getCommentList.list, source).then(res => {
			// console.log(res, 111122)
			var total1=0
			var total2=0
			var total3=0
			for (var i = 0; i < res.rows.length; i++) {
				res.rows[i].createTime = getTimeLeft(res.rows[i].createTime)
				// debugger
				
				if(res.rows[i].status==3){
					 total1++
					 // console.log(total1)
					this.data.tasteList[1].name='好评('+total1+')'
				}else if(res.rows[i].status==2){
					 total2 +=1
					 this.data.tasteList[2].name='中评('+total2+')'
				}else if(res.rows[i].status==1){
					 total3 +=1
					 this.data.tasteList[3].name='差评('+total3+')'
				}
			}
			this.setData({
				commentList: res.rows,
				tasteList:this.data.tasteList
			})
			if(this.data.id==0){
				this.setData({
					all:res.rows.length
				})
				this.data.tasteList[0].name='全部评论('+this.data.all+')'
			}
			var pages=res.total/this.data.searchGood.size
			this.setData({
			  searchGood: {
			    page: this.data.searchGood.page,
			    size: this.data.searchGood.size,
			    pages: pages
			  }
			})			
			for(var i=0;i<res.rows.length;i++){
				var pList=res.rows[i].commentPhoto
				if(res.rows[i].commentPhoto!=null){
					var photoList=(res.rows[i].commentPhoto.split(','))
						res.rows[i]['photoList']=photoList
				}
			}
			this.setData({
				commentList: res.rows,
				tasteList:this.data.tasteList
			})
			// console.log(this.data.commentList)
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
		// this.setData({
		// 	isRequest: true
		// })
	},
	
})
